package com.hexacta.hrs.test.data;

import java.util.Map;

import org.hibernate.Session;
import org.vectrics.common.db.CodedObject;
import org.vectrics.common.db.RecordObject;

import com.hexacta.hrs.test.data.TestDataCreator.ObjectReference;

/**
 * Helper methods for TestDataCreator and TestDataDependencyCreator instances,
 * in order to avoid duplicate code.
 * 
 * @author tpollak
 */
class DataCreatorHelper {
    public Object loadObjectFromContext(Map<String, Object> context, String key, Session currentSession) {
        if (context.containsKey(key)) {
            ObjectReference reference = (ObjectReference) context.get(key);
            return currentSession.load(reference.getObjectClass(), reference.getObjectId());
        }
        throw new IllegalArgumentException("Object " + key + " not found in context.");
    }

    public void storeObjectReferenceInContext(Map<String, Object> context, String key, CodedObject object) {
        ObjectReference reference = new ObjectReference(object);
        context.put(key, reference);
    }

    public void storeObjectReferenceInContext(Map<String, Object> context, String key, RecordObject object) {
        ObjectReference reference = new ObjectReference(object);
        context.put(key, reference);
    }

}
